package com.effectiv.gooruda.mvc.argumentresolver;

import java.util.ArrayList;
import java.util.List;

import org.codehaus.jackson.annotate.JsonProperty;

import org.springframework.validation.FieldError;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@ToString
public class AjaxResponse {

	public static final String SUCCESS = "SUCCESS";
	public static final String FAIL = "FAIL";
	public static final String ERROR = "ERROR";
	

	@JsonProperty
	@Getter
	@Setter
	private String status = SUCCESS;

	@JsonProperty
	@Getter
	@Setter
	private Object result;

	@JsonProperty
	@Getter
	@Setter
	private String message;
	
	@Deprecated
	public void buildError(List<FieldError> fieldErrors) {
		status = FAIL;
		if (fieldErrors != null) {
			List<ErrorMessage> errors = new ArrayList<ErrorMessage>();
			for (FieldError e : fieldErrors) {
				errors.add(new ErrorMessage(e.getField(), e.getField() + " "
						+ e.getDefaultMessage()));
			}

			result = errors;
		}
	}
	
	public void markValidationFailure(List<FieldError> fieldErrors) {
		status = FAIL;
		if (fieldErrors != null) {
			List<ErrorMessage> errors = new ArrayList<ErrorMessage>();
			for (FieldError e : fieldErrors) {
				errors.add(new ErrorMessage(e.getField(), e.getField() + " "
						+ e.getDefaultMessage()));
			}

			result = errors;
		}
	}
}
